using System;
using System.Data;
using System.ComponentModel;
using CodeSmith.Engine;
using SchemaExplorer;

public class Common : CodeTemplate
{
	#region Name String Format
	
    public string PascalCase(string source)
    {
        return source.Substring(0, 1).ToUpper() + source.Substring(1).Replace(" ", "");
    }
    public string CamelCase(string source)
    {
        return source.Substring(0, 1).ToLower() + source.Substring(1).Replace(" ", "");
    }
	
	public string NormalizeName(string value)
	{
		return value.Replace(" ", "").Replace("-", "_");
	}
	
	public string ClassName(string name)
	{
		return NormalizeName(name);
	}
	
	public string PropertyName(string name)
	{
		return NormalizeName(name);
	}
	
	#endregion
	
	#region DbType Convertion
	
	public string DbType2SystemType(DbType dType, bool nullable)
	{
		string nullableStr = nullable ? "?" : "";
		switch(dType)
		{
			case DbType.AnsiString:
			case DbType.AnsiStringFixedLength:
				return "string";
			case DbType.Binary:
				return "byte[]" + nullableStr;
			case DbType.Boolean:
				return "bool" + nullableStr;
			case DbType.Byte:
				return "byte";
			case DbType.Currency:
				return "decimal" + nullableStr;
			case DbType.Date:
				return "DateTime" + nullableStr;
			case DbType.DateTime:
				return "DateTime" + nullableStr;
			case DbType.Decimal:
				return "decimal" + nullableStr;
			case DbType.Double:
				return "double" + nullableStr;
			case DbType.Guid:
				return "Guid" + nullableStr;
			case DbType.Int16:
				return "short" + nullableStr;
			case DbType.Int32:
				return "int" + nullableStr;
			case DbType.Int64:
				return "long" + nullableStr;
			case DbType.Object:
				return "object";
			case DbType.SByte:
				return "sbyte" + nullableStr;
			case DbType.Single:
				return "float" + nullableStr;
			case DbType.String:
			case DbType.StringFixedLength:
				return "string";
			case DbType.Time:
				return "TimeSpan" + nullableStr;
			case DbType.UInt16:
				return "ushort" + nullableStr;
			case DbType.UInt32:
				return "uint" + nullableStr;
			case DbType.UInt64:
				return "ulong";
			case DbType.VarNumeric:
				return "decimal" + nullableStr;
			case DbType.Xml:
				return "System.Linq.Xml";
			default:
				return "__UNKNOWN__" + dType;
		}
	}
	
	public string GetConvert(string colName,DbType dbType)
	{
		switch(dbType)
		{	
			case DbType.String:
			case DbType.StringFixedLength:
			case DbType.AnsiString:
			case DbType.AnsiStringFixedLength:
				return "Convert.ToString("+colName+")";
			case DbType.Date:
				return "Convert.ToDateTime("+colName+")";
			case DbType.DateTime:
				return "Convert.ToDateTime("+colName+")";
			case DbType.Boolean:
				return "Convert.ToBoolean("+colName+")";
			case DbType.Currency:
				return "Convert.ToDecimal("+colName+")";
			case DbType.Decimal:
				return "Convert.ToDecimal("+colName+")";
			case DbType.Double:
				return "Convert.ToDouble("+colName+")";
			case DbType.Int16:
				return "Convert.ToInt16("+colName+")";
			case DbType.Int32:
				return "Convert.ToInt32("+colName+")";
			case DbType.Int64:
				return "Convert.ToInt64("+colName+")";
			case DbType.SByte:
				return "Convert.ToByte("+colName+")";
			case DbType.Single:
				return "Convert.ToSingle("+colName+")";
			case DbType.UInt16:
				return "Convert.ToUInt16("+colName+")";
			case DbType.UInt32:
				return "Convert.ToUInt32("+colName+")";
			case DbType.UInt64:
				return "Convert.ToUInt64("+colName+")";
			case DbType.VarNumeric:
				return "Convert.ToDecimal("+colName+")";
			case DbType.Guid:
				return "new Guid(" + colName + ")";
			default:
				return "Convert.ToString("+colName+")";
		}
	}

	#endregion
	
	public string FormatColumns(ColumnSchemaCollection columns, Func<ColumnSchema, string> formater, string spliter)
	{
		string str = "";

		for(int i=0; i < columns.Count; i++)
		{
			ColumnSchema col = columns[i];
			
			str += formater(col);
			if(i < columns.Count - 1)
				str += spliter;
		}
		return str;
	}		
}